package com.beiding.workordersystem.system.controller;

import com.beiding.workordersystem.common.standard.R;
import com.beiding.workordersystem.system.entity.FileEntity;
import com.beiding.workordersystem.system.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

//但文件上传
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    R upload(@RequestPart("file") MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String name = file.getOriginalFilename();
        if (name == null) {
            name = file.getName();
        }
        FileEntity serverFile = new FileEntity();
        serverFile.setName(name);
        fileService.save(serverFile, inputStream);
        return R.ok(serverFile.getId());
    }

    @GetMapping("/download/{id}")
    void download(@PathVariable String id) {
        fileService.download(id);
    }

    @GetMapping("/delete")
    R delete(@RequestParam String id) {
        fileService.delete(id);
        return R.ok();
    }

}
